package com.team2.outpatient_sys.web.pharmacyControllers;

import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.service.PersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/personals")
public class PersonalController {
    @Autowired
    private PersonalService personalService;

    //    根据名字来查询自己的个人信息
    @GetMapping("/showPersonalByName/{name}")
    public Result showPersonalByName(@PathVariable String name){
        Result result = new Result();
        Doctor doctor = personalService.findByName(name);
        if(doctor != null ){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(doctor);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
    //旧密码确认
    @PostMapping("/showPersonalByIdAndPass")
    public Result showPersonalByIdAndPass(@RequestBody Doctor doctor){
        Result result = new Result();
        Doctor dd = personalService.findByIdAndPass(doctor.getId(),doctor.getPassword());
        if(dd != null ){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(doctor);
        }else{
            result.setCode(422);
            result.setMsg("密码错误");
        }
        return result;
    }
    //修改密码
    @PutMapping("/updateNewPass")
    public Result updateNewPass(@RequestBody Doctor doctor){
        Result result = new Result();
        Integer dd = personalService.updateNewPass(doctor.getPassword(),doctor.getId());
        if(dd >= 1 ){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    //查询用户名是否重复
    @GetMapping("/findByUsername/{username}")
    public Result findByUsername(@PathVariable String username){
        Result result = new Result();
        Doctor doctor = personalService.findByUsername(username);
        if (doctor != null ){
            result.setCode(422);
            result.setMsg("用户名重复");
        }else {
            result.setCode(200);
            result.setMsg("用户名可用");
        }
        return result;
    }

    //修改个人资料
    @PutMapping("/updatePersonal")
    public Result updatePersonal(@RequestBody Doctor doctor){
        Result result = new Result();
        Integer dd = personalService.updatePersonal(doctor);
        if(dd >= 1 ){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }
}
